//
//  BK_Pay_Tool.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/23.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_Pay_Tool.h"
#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"
#import "BKODWXManager.h"

@implementation BK_Pay_Tool
/*
 接口名称：	pay_limit-info
 接口用途：	获取移动端支付限制信息
 method：	POST
 接口地址：	pay/limit-info
 输入参数：	无
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 "data":
 {
 "shopping_ticket_pay": "true/false //是否显示购物券支付",
 "global_order_alipay": "true/false //全球购是否支付宝支付，false为平台账户支付",
 "global_order_shopping_ticket_pay": "true/false //全球购订单是否支持购物券",
 "global_order_sugar_bowl_pay": "true/false //全球购订单是否支持糖罐账户支付",
 "sugar_bowl_pay": "true/false //是否显示糖罐账户支付",
 " inland_order_alipay": "true/false //国内商品是否支持支付宝支付",
 "inland_wx_pay": "true/false //国内商品是否支持微信支付",
 }
 }
 */
+(void)BK_Pay_Limit_Info:(BK_Pay_Callback)CallBack{
    kOrderLoading;
    NSString *const ODPayLimitInfo = @"pay/limit-info";
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(ODPayLimitInfo) parameters:nil
                                    success:^(id  _Nullable responseObject){
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (!success) {
                                            kOrderDismiss;
                                            kOrderPromptMsg(responseObject[@"msg"]);
                                            return;
                                        }else{
                                            kOrderDismiss;
                                            CallBack(YES, responseObject,nil);
                                        }
                                        
                                    }failure:^(id  _Nullable responseObject, NSError * _Nullable error){
                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                        CallBack(NO, nil,error);
                                    }];
}
/*
 接口名称：	account_balance
 接口用途：	查询账户信息
 method：	POST
 接口地址：	/account/balance
 输入参数：	无
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "errorCode": "xxx //返回错误编码",
 "data":
 {
 "account":
 [
 {
 "account_balance": "xxx//账户余额",
 "account_type": "xxx//账户类型(001 财富账户，002 申购金账户，012 糖罐账户)",
 "account_type_name": "xxx//账户类型名称 (申购金账户和财富账户)如果是非会员用户直接显示账户余额，且只有申购金账户注意如果是糖罐账户返回的是人民币，但是界面得显示颗，一元=10颗糖",
 }
 ],
 "is_full_member": "0/1 //是否是会员 1:是会员 0：非会员",
 }
 }
 备注：
 */
+(void)BK_Account_Balance_Info:(BK_Pay_Callback)CallBack{
    NSString *const ODAccountBalance = @"account/balance";
    kOrderLoading;
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(ODAccountBalance) parameters:nil
                                    success:^(id  _Nullable responseObject){
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (!success) {
                                            kOrderDismiss;
                                            kOrderPromptMsg(responseObject[@"msg"]);
                                            return;
                                        }else{
                                            kOrderDismiss;
                                            CallBack(YES, responseObject,nil);
                                        }
                                        
                                    }failure:^(id  _Nullable responseObject, NSError * _Nullable error){
                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                        CallBack(NO, nil,error);
                                    }];
}




/*
 接口名称：	_pay_calc-actually-amount
 接口用途：	计算订单的实付金额
 method：	POST
 接口地址：	/pay/calc-actually-amount
 输入参数：
 key	参数类型	是否必须	参数解释
 order_no	int	是	订单号
 sugar_amount	float	是	糖罐金额
 shopping_tickets		是	购物券,多个以英文逗号分隔
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 "data":
 {
 " actuallyAmount": "xxxx//实付金额",
 }
 }
 备注：
 请求参数：
 order_no:xxxx //订单号 sugar_amount:xxx //糖罐金额 shopping_tickets //购物券,多个以英文逗号分隔
 
 */

+(void)BK_PayCalc_Actually_Amount:(BK_Pay_Callback)CallBack WithOrderNum:(NSString *)orderNumber{
    NSString *const ODRealPayAmount = @"pay/calc-actually-amount";
    kOrderLoading;
    NSDictionary *paramDic = @{
                               @"order_no"          : orderNumber,
                               @"shopping_tickets"  : @"",
                               @"sugar_amount"      : @"0.0"
                               };
    
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(ODRealPayAmount) parameters:paramDic
     
                                    success:^(id  _Nullable responseObject){
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (!success) {
                                            kOrderDismiss;
                                            kOrderPromptMsg(responseObject[@"msg"]);
                                            return;
                                        }else{
                                            kOrderDismiss;
                                            CallBack(YES, responseObject,nil);
                                        }
                                    } failure:^(id  _Nullable responseObject, NSError * _Nullable error){
                                        
                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                        CallBack(NO, nil,error);
                                    }];
    
    
}
/*
 订单总金额计算
 */



+(void)BK_PayCalc_Order_Total:(BK_Pay_Callback)CallBack WithOrderNum:(NSString *)orderNumber{
    NSString * const strOrder_total = @"order/total";
    kOrderLoading;
    NSDictionary *paramDic = @{
                               @"order_no"          : orderNumber,
                               @"shopping_tickets"  : @"",
                               @"sugar_amount"      : @"0.0"
                               };
    
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(strOrder_total) parameters:paramDic
     
                                    success:^(id  _Nullable responseObject){
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (!success) {
                                            kOrderDismiss;
                                            kOrderPromptMsg(responseObject[@"msg"]);
                                            return;
                                        }else{
                                            kOrderDismiss;
                                            CallBack(YES, responseObject,nil);
                                        }
                                    } failure:^(id  _Nullable responseObject, NSError * _Nullable error){
                                        
                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                        CallBack(NO, nil,error);
                                    }];
    
    
}


























/*
 接口名称：	order_canPayTimes
 接口用途：	获取订单剩余可支付时间
 method：	POST
 接口地址：	order/canPayTimes
 输入参数：
 key	参数类型	是否必须	参数解释
 order_no	int	是	订单编号
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "errorCode": "xxx //返回错误编码",
 "data":
 {
 " canPayTimes": "xxx //返回总剩余可支付秒数，已超时的返回0",
 }
 }
 备注：
 请求参数：
 order_no: xxx // 订单编号
 */
+(void)BK_Order_CanPayTimes:(BK_Pay_Callback)CallBack WithOrderNum:(NSString *)orderNumber{
    NSString *const ODValidTime = @"order/canPayTimes";
    //    kOrderLoading;
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(ODValidTime) parameters:@{@"order_no" : orderNumber}
     
                                    success:^(id  _Nullable responseObject){
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (!success) {
                                            //                                            kOrderDismiss;
                                            kOrderPromptMsg(responseObject[@"msg"]);
                                            CallBack(NO, nil,nil);
                                            //                                            return;
                                        }else{
                                            //                                            kOrderDismiss;
                                            CallBack(YES, responseObject,nil);
                                        }
                                        
                                    }failure:^(id  _Nullable responseObject, NSError * _Nullable error){
                                        
                                        //                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                        CallBack(NO, nil,error);
                                        
                                    }];
}




/*
 
 接口名称：	pay_order
 接口用途：	提交付款信息
 method：	POST
 接口地址：	pay/order
 输入参数：
 key	参数类型	是否必须	参数解释
 order_no	int	是	多个以逗号分隔
 account_type	int	是	001/002账户类型code
 shopping_ticket_code	string	是	xxx,xxx多个以逗号分隔
 sugar_amount	float	是	糖罐账户支付（单位：元）
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 }
 备注：
 请求参数：
 order_no=xxx,xxx多个以逗号分隔 account_type=001/002账户类型code shopping_ticket_code=xxx,xxx多个以逗号分隔 sugar_amount=xxxx//糖罐账户支付（单位：元）
 
 */

+(void)BK_Pay_order:(BK_Pay_Callback)CallBack WithOrderNum:(NSString *)orderNumber{
    kOrderLoading;
    NSString *account_type = @"002";
    NSString *const ODAccountBalancePay = @"pay/order";
    NSDictionary *paramDic = @{
                               @"order_no"              : orderNumber,
                               @"account_type"          : account_type,
                               @"shopping_ticket_code"  : @"",
                               @"sugar_amount"          : @"0.0"
                               };
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(ODAccountBalancePay) parameters:paramDic
                                    success:^(id  _Nullable responseObject){
                                        
                                        kOrderDismiss;
                                        
                                        
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (!success) {
                                            
                                            kOrderPromptMsg(responseObject[@"msg"]);
                                            return;
                                        }
                                        
                                        
                                        
                                        CallBack(YES, responseObject,nil);
                                        
                                    }failure:^(id  _Nullable responseObject, NSError * _Nullable error){
                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                    }];
    
}



/*
 
 接口名称：	pay_inland-alipay-info
 接口用途：	获取国内订单支付宝信息
 method：	POST
 接口地址：	pay/inland-alipay-info
 输入参数：
 key	参数类型	是否必须	参数解释
 order_no	int	是	订单号
 sugar_amount	float	是	糖罐金额
 shopping_tickets		是	购物券
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 "data":
 {
 " pay_info": "xxx",
 }
 }
 备注：
 请求参数：
 order_no:xxxx //订单号 sugar_amount:xxx //糖罐金额 shopping_tickets //购物券
 
 注意：当success为true的情况下pay_info为空字符串时，表示支付金额小于等于0或者国内支付宝开关关闭，这个时候应该请求原有的平台支付接口进行支付.
 
 */


+(void)BK_Pay_Inland_Alipay_Info:(BK_Pay_Callback)CallBack WithOrderNum:(NSString *)orderNumber{
    NSString *const ODAliPay_inland = @"pay/inland-alipay-info";
    
    kOrderLoading;
    
    NSMutableDictionary *paramDic = [NSMutableDictionary dictionary];
    
    [paramDic setObject:@(0) forKey:@"sugar_amount"];
    [paramDic setObject:@"" forKey:@"shopping_tickets"];
    [paramDic setObject:orderNumber forKey:@"order_no"];
    
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(ODAliPay_inland) parameters:paramDic.copy
                                    success:^(id  _Nullable responseObject){
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (!success) {
                                            kOrderDismiss;
                                            kOrderPromptMsg(responseObject[@"msg"]);
                                            return;
                                        }else{
                                            kOrderDismiss;
                                            CallBack(YES, responseObject,nil);
                                        }
                                        
                                    }failure:^(id  _Nullable responseObject, NSError * _Nullable error){
                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                    }];
    
    
}
+(void)BK_AliPay:(BK_Pay_Callback)CallBack WithOrderNum:(NSString *)orderNumber andGlobalStatus:(NSString*)globalStatus{
    if ([globalStatus isEqualToString:@"1"]){//全球购
        [BK_Pay_Tool BK_Pay_Global_Alipay_Info:^(BOOL isSuccess, id obj, NSError *error) {
            if (isSuccess) {
                CallBack(YES, obj,nil);
            }
        } WithOrderNum:orderNumber];
    }else if ([globalStatus isEqualToString:@"0"]){//国内
        [BK_Pay_Tool BK_Pay_Inland_Alipay_Info:^(BOOL isSuccess, id obj, NSError *error) {
            if (isSuccess) {
                CallBack(YES, obj,nil);
            }
        } WithOrderNum:orderNumber];
    }
}

/*
 接口名称：	pay_global-alipay-info
 接口用途：	获取全球购支付宝信息
 method：	POST
 接口地址：	pay/global-alipay-info
 输入参数：
 key	参数类型	是否必须	参数解释
 order_no	int	是	订单号
 shopping_tickets	string	是	购物券
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 "data":
 {
 " pay_info": "xxxx",
 }
 }
 备注：
 请求参数：
 order_no:xxxx订单号 shopping_tickets //购物券
 */

+(void)BK_Pay_Global_Alipay_Info:(BK_Pay_Callback)CallBack WithOrderNum:(NSString *)orderNumber{
    NSString *const ODAliPay_global = @"pay/global-alipay-info";
    kOrderLoading;
    NSMutableDictionary *paramDic = [NSMutableDictionary dictionary];
    
    [paramDic setObject:@"" forKey:@"shopping_tickets"];
    [paramDic setObject:orderNumber forKey:@"order_no"];
    
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(ODAliPay_global) parameters:paramDic.copy
                                    success:^(id  _Nullable responseObject){
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (!success) {
                                            kOrderDismiss;
                                            kOrderPromptMsg(responseObject[@"msg"]);
                                            return;
                                        }else{
                                            kOrderDismiss;
                                            CallBack(YES, responseObject,nil);
                                        }
                                        
                                    }failure:^(id  _Nullable responseObject, NSError * _Nullable error){
                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                    }];
}








/*
 接口名称：	get_wx_pay_info
 接口用途：	获得微信支付信息
 method：	POST
 接口地址：	pay/wx-pay-info
 输入参数：
 key	参数类型	是否必须	参数解释
 order_no	string	是	订单编号
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 "data":
 {
 " pay_info": "xxx",
 }
 }
 备注：
 */


+(void)BK_WX_Pay_Info:(BK_Pay_Callback)CallBack WithOrderNum:(NSString *)orderNumber{
    
    if (![[BKODWXManager manager] isWXAppInstalled]) {
        kOrderPromptMsg(@"未安装微信");
        return;
    }
    NSString *const ODWXPayInfo = @"pay/wx-pay-info";
    kOrderLoading;
    NSDictionary *paramDic = @{@"order_no":orderNumber};
    
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:kOrderURL(ODWXPayInfo) parameters:paramDic
                                    success:^(id  _Nullable responseObject){
                                        BOOL success = [responseObject[@"success"] boolValue];
                                        if (!success) {
                                            kOrderDismiss;
                                            kOrderPromptMsg(responseObject[@"msg"]);
                                            return;
                                        }else{
                                            kOrderDismiss;
                                            CallBack(YES, responseObject,nil);
                                        }
                                    }failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
                                        kOrderDismiss;
                                        NSString *errorMsg;
                                        if (error.code == -1001) {
                                            errorMsg = @"请求超时";
                                        }else if(error.code == 502){
                                            errorMsg = @"服务器开小差了，请稍后再试！";
                                        }else{
                                            errorMsg = @"请求失败";
                                        }
                                        kOrderPromptMsg(errorMsg);
                                    }];
    
}






@end





















